package com.zhangzhan.controller;

import com.zhangzhan.domain.commcon.ResponseResult;
import com.zhangzhan.domain.entity.User;
import com.zhangzhan.domain.enums.AppHttpCodeEnum;
import com.zhangzhan.exception.MySystemException;
import com.zhangzhan.service.BlogLoginService;
import io.jsonwebtoken.lang.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BlogLoginController {
    @Autowired
    private BlogLoginService blogLoginService;

    // 用户登录接口
    @PostMapping("/login")
    public ResponseResult login(@RequestBody User user){
        if(!Strings.hasText(user.getUserName())){
            //如果没有传用户名过来则抛出异常信息
            throw new MySystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
        }
        return blogLoginService.login(user);
    }

    //用户退出登录
    @PostMapping("/logout")
    public ResponseResult logout(){
        //调用service层
       return blogLoginService.logout();
    }
}